<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 联通年度收入
 *
 * @icon fa fa-circle-o
 */
class Ltcaiwushow extends Backend
{

    /**
     * Ltcaiwushow模型对象
     * @var \app\admin\model\Ltcaiwushow
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Ltcaiwushow;
        $this->view->assign("ctypeList", $this->model->getCtypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $input = input('param.');
        $filter = isset($input['filter'])?json_decode($input['filter'],true):'';
        if(empty($filter['year'])){
            $year = date('Y')-1;
            $year = 2024;
        }else{
            $year = $filter['year'];
        }

        $list = $this->model
            //->where($where)
            //->order($sort, $order)
            ->order('id asc')
            ->paginate(30);
        foreach($list as $row){
            if($row['shouru'] == '佣金'){
                $row->yue1 = ltyj_find(1,$year);
                $row->yue2 = ltyj_find(2,$year);
                $row->yue3 = ltyj_find(3,$year);
                $row->yue4 = ltyj_find(4,$year);
                $row->yue5 = ltyj_find(5,$year);
                $row->yue6 = ltyj_find(6,$year);
                $row->yue7 = ltyj_find(7,$year);
                $row->yue8 = ltyj_find(8,$year);
                $row->yue9 = ltyj_find(9,$year);
                $row->yue10 = ltyj_find(10,$year);
                $row->yue11 = ltyj_find(11,$year);
                $row->yue12 = ltyj_find(12,$year);
            }
            if($row['shouru'] == '移机'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'yiji');
                }
            }
            if($row['shouru'] == '光猫'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'guangmao');
                }
            }
            if($row['shouru'] == '装机费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'zhuangji');
                }
            }
            if($row['shouru'] == '续费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'xufei');
                }
            }
            if($row['shouru'] == '其他'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'ru_other');
                }
            }
            if($row['shouru'] == '合计收入:'){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $arr = [ $list[1][$yue], $list[2][$yue], $list[3][$yue], $list[4][$yue], $list[5][$yue]];
                    $sum = array_sum($arr);
                    $row->$yue = $sum==0?'':float_two($sum)+0;
                }
            }
            if($row['shouru'] == '批扣'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'pikou');
                }
            }
            if($row['shouru'] == '档口租金'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'zujin');
                }
            }
            if($row['shouru'] == '装移机费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'zhi_yiji');
                }
            }
            if($row['shouru'] == '购买光猫'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'zhi_guangmao');
                }
            }
            if($row['shouru'] == '退猫'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'tuimao');
                }
            }
            if($row['shouru'] == '档口其他杂费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'zhi_other');
                }
            }
            if($row['shouru'] == '工资'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'gz');
                }
            }
            if($row['shouru'] == '通兆税费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'ttsf');
                }
            }
            if($row['shouru'] == '社保费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'sbf');
                }
            }
            if($row['shouru'] == '对公户杂费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'duigong');
                }
            }
            if($row['shouru'] == '室分电费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'dianfei');
                }
            }
            if($row['shouru'] == '合计支出：'){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $arr = [
                        $list[8][$yue],$list[9][$yue], $list[10][$yue], $list[11][$yue], $list[12][$yue],
                        $list[13][$yue], $list[14][$yue], $list[15][$yue], $list[16][$yue],$list[17][$yue],
                        $list[18][$yue],
                        ];
                    $sum = array_sum($arr);
                    $row->$yue = $sum==0?'':float_two($sum)+0;
                }
            }
            if(strpos($row['shouru'],'对公户其他收入') !== false){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ltyj_find($v,$year,'dg_shouru');
                }
            }
            if(strpos($row['shouru'],'利润') !== false){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $n = (float)$list[0][$yue]+(float)$list[6][$yue]-(float)$list[19][$yue]+(float)$list[20][$yue];
                    $row->$yue = $n?float_two($n):'';
                }
            }
            $row->year = $year;
            $heji = array_sum([
                $row->yue1,$row->yue2,$row->yue3,$row->yue4,$row->yue5,$row->yue6,
                $row->yue7,$row->yue8,$row->yue9,$row->yue10,$row->yue11,$row->yue12]);
            $row->heji = float_two($heji);
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];

        return json($result);
    }


}
